{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #按规则计算统计结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: statisticalResult"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #按规则计算统计结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>为了深入了解这些生物群体的生态特征，你们进行了大量的实地观察和数据采集。数组 <code>arrayA</code> 记录了各个生物群体数量数据，其中 <code>arrayA[i]</code> 表示第 <code>i</code> 个生物群体的数量。请返回一个数组 <code>arrayB</code>，该数组为基于数组 <code>arrayA</code> 中的数据计算得出的结果，其中 <code>arrayB[i]</code> 表示将第 <code>i</code> 个生物群体的数量从总体中排除后的其他数量的乘积。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arrayA = [2, 4, 6, 8, 10]\n",
    "<strong>输出：</strong>[1920, 960, 640, 480, 384]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有元素乘积之和不会溢出 32 位整数</li>\n",
    "\t<li><code>arrayA.length &lt;= 100000</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [gou-jian-cheng-ji-shu-zu-lcof](https://leetcode.cn/problems/gou-jian-cheng-ji-shu-zu-lcof/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [gou-jian-cheng-ji-shu-zu-lcof](https://leetcode.cn/problems/gou-jian-cheng-ji-shu-zu-lcof/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,4,6,8,10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        cnt_zero = 0\n",
    "        product = 1\n",
    "        for num in arrayA:\n",
    "            if num == 0:\n",
    "                cnt_zero += 1\n",
    "            else:\n",
    "                product *= num\n",
    "        \n",
    "        if cnt_zero == 0:\n",
    "            for i in range(len(arrayA)):\n",
    "                arrayA[i] = product // arrayA[i]\n",
    "            return arrayA\n",
    "\n",
    "        if cnt_zero == 1:\n",
    "            for i in range(len(arrayA)):\n",
    "                if arrayA[i] == 0:\n",
    "                    arrayA[i] = product\n",
    "                else:\n",
    "                    arrayA[i] = 0\n",
    "            return arrayA\n",
    "        \n",
    "        return [0 for i in range(len(arrayA))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        zero = [0 for i in range(len(arrayA))]\n",
    "        all_mul = 1\n",
    "        for i in range(len(arrayA)):\n",
    "            if arrayA[i] != 0:\n",
    "                all_mul *= arrayA[i]\n",
    "            else:\n",
    "                zero[i] = 1\n",
    "        sum_zeor = sum(zero)\n",
    "        for i in range(len(arrayA)):\n",
    "            if arrayA[i] == 0:\n",
    "                if sum_zeor == 1:\n",
    "                    arrayA[i] = all_mul\n",
    "            else:\n",
    "                if sum_zeor > 0:\n",
    "                    arrayA[i] = 0\n",
    "                else:\n",
    "                    arrayA[i] = int(all_mul / arrayA[i])\n",
    "        return arrayA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        zero = 0\n",
    "        all_mul = 1\n",
    "        for i in range(len(arrayA)):\n",
    "            if arrayA[i] != 0:\n",
    "                all_mul *= arrayA[i]\n",
    "            else:\n",
    "                zero += 1\n",
    "        for i in range(len(arrayA)):\n",
    "            if arrayA[i] == 0:\n",
    "                if zero == 1:\n",
    "                    arrayA[i] = all_mul\n",
    "            else:\n",
    "                if zero > 0:\n",
    "                    arrayA[i] = 0\n",
    "                else:\n",
    "                    arrayA[i] = int(all_mul / arrayA[i])\n",
    "        return arrayA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        allmulit = 1\n",
    "        for i in arrayA:\n",
    "            if i == 0:\n",
    "                allmulit *= 1\n",
    "            else:\n",
    "                allmulit *= i\n",
    "\n",
    "        if arrayA.count(0) == 1 :\n",
    "            for i in range(len(arrayA)):\n",
    "                if arrayA[i] == 0:\n",
    "                    arrayA[i] = allmulit\n",
    "                else:\n",
    "                    arrayA[i] = 0\n",
    "                # arrayA[i] = allmulit // arrayA[i]\n",
    "            return arrayA\n",
    "        \n",
    "        if arrayA.count(0) > 1 :\n",
    "            return [0] * len(arrayA)\n",
    "\n",
    "        for i in range(len(arrayA)):\n",
    "            arrayA[i] = allmulit // arrayA[i]\n",
    "        return arrayA\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        sum=flag=1\n",
    "        for i in arrayA:\n",
    "            if i:\n",
    "                sum*=i\n",
    "            else:\n",
    "                flag=0\n",
    "        if arrayA.count(0)==0:\n",
    "            for i in range(len(arrayA)):\n",
    "                arrayA[i]=int(sum/arrayA[i])\n",
    "        elif arrayA.count(0)==1:\n",
    "            for i in range(len(arrayA)):\n",
    "                arrayA[i]=0 if arrayA[i] else sum\n",
    "        else:\n",
    "            for i in range(len(arrayA)):\n",
    "                arrayA[i]=0 \n",
    "        return arrayA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        chengji = 1\n",
    "        count0 = 0\n",
    "        for i in arrayA:\n",
    "            if i == 0:\n",
    "                count0 += 1\n",
    "                continue\n",
    "            chengji *= i\n",
    "        if count0 >1:\n",
    "            return [0]*len(arrayA)\n",
    "        elif count0 == 1:\n",
    "            res = [0 if i != 0 else chengji for i in arrayA ]\n",
    "        else:\n",
    "            res = [chengji//i for i in arrayA]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        arrayB, tmp = [1] * len(arrayA), 1\n",
    "        for i in range(1, len(arrayA)):\n",
    "            arrayB[i] = arrayB[i - 1] * arrayA[i - 1]\n",
    "        for i in range(len(arrayA) - 2, -1, -1):\n",
    "            tmp *= arrayA[i + 1]\n",
    "            arrayB[i] *= tmp\n",
    "        return arrayB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        x=1\n",
    "        c=1\n",
    "        b=0\n",
    "        arrayB=[]\n",
    "        for i in range(len(arrayA)):\n",
    "               x=x*arrayA[i]\n",
    "               if arrayA[i]!=0:\n",
    "                  c=c*arrayA[i]\n",
    "               if arrayA[i]==0:\n",
    "                  b+=1\n",
    "        if b>1:\n",
    "            arrayB=[0]*len(arrayA)\n",
    "            return arrayB\n",
    "        else:\n",
    "            for a in range(len(arrayA)):\n",
    "               if arrayA[a]==0:\n",
    "                 arrayB.append(c)\n",
    "               else:\n",
    "                   d=int(x/arrayA[a])\n",
    "                   arrayB.append(d)\n",
    "        return arrayB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        size = len(arrayA)\n",
    "        arrayB = [1 for _ in range(size)]\n",
    "        temp = 1\n",
    "        \n",
    "        for i in range(1, size):\n",
    "            arrayB[i] = arrayB[i - 1] * arrayA[i - 1]\n",
    "           \n",
    "        for i in range(size - 2, -1, -1):\n",
    "            temp *= arrayA[i + 1]\n",
    "            arrayB[i] *= temp\n",
    "           \n",
    "        return arrayB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        temp,array = 1,[1]*len(arrayA) \n",
    "        for i in range(1,len(arrayA)):\n",
    "            array[i] = array[i-1]*arrayA[i-1]\n",
    "        for j in range(len(arrayA)-2,-1,-1):\n",
    "            temp *= arrayA[j+1]\n",
    "            array[j] *= temp\n",
    "        return array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        arrayB, tmp = [1] * len(arrayA), 1\n",
    "        for i in range(1, len(arrayA)):\n",
    "            arrayB[i] = arrayB[i-1]*arrayA[i-1]\n",
    "        for i in range(len(arrayA) - 2, -1, -1):\n",
    "            tmp *= arrayA[i+1]\n",
    "            arrayB[i] *= tmp\n",
    "        return arrayB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        arrayB,tmp=[1]*len(arrayA),1\n",
    "        for i in range(1,len(arrayA)):\n",
    "            arrayB[i]=arrayB[i-1]*arrayA[i-1]\n",
    "        for i in range(len(arrayA)-2,-1,-1):\n",
    "            tmp=tmp*arrayA[i+1]\n",
    "            arrayB[i]=arrayB[i]*tmp\n",
    "        return arrayB\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        arrayB,tmp=[1]*len(arrayA),1\n",
    "        for i in range(1,len(arrayA)):\n",
    "            arrayB[i]=arrayB[i-1]*arrayA[i-1]\n",
    "        for i in range(len(arrayA)-2,-1,-1):\n",
    "            tmp=tmp*arrayA[i+1]\n",
    "            arrayB[i]=tmp*arrayB[i]\n",
    "        return arrayB\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        m = 1\n",
    "        r0 = []\n",
    "        for i in range(len(arrayA)):\n",
    "            if arrayA[i] == 0:\n",
    "                r0.append(i)\n",
    "            else:\n",
    "                m *= arrayA[i]\n",
    "\n",
    "        if len(r0)>1:\n",
    "            tmp = [0]*len(arrayA)\n",
    "            return tmp\n",
    "        elif len(r0) == 1:\n",
    "            tmp = [0]*len(arrayA)\n",
    "            tmp[r0[0]] = m\n",
    "            return tmp\n",
    "\n",
    "        res = []\n",
    "        for j in range(len(arrayA)):\n",
    "            res.append(int(m/arrayA[j]))\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import *\n",
    "from heapq import *\n",
    "from sys import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from string import *\n",
    "from bisect import *\n",
    "from graphlib import *\n",
    "from sortedcontainers import *\n",
    "from copy import *\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arr: List[int]) -> List[int]:\n",
    "        t = 1\n",
    "        if arr.count(0) > 1: return [0] * len(arr)\n",
    "        if arr.count(0) == 1:\n",
    "            for x in arr: \n",
    "                if x != 0: t *= x\n",
    "            return [0 if x != 0 else t for x in arr]\n",
    "        for x in arr: t *= x\n",
    "        return [t // x for x in arr]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        left = [1] * n\n",
    "        right = [1] * n\n",
    "        for i in range(1, n):\n",
    "            left[i] = left[i - 1] * arrayA[i - 1]\n",
    "        for i in range(n - 2, -1, -1):\n",
    "            right[i] = right[i + 1] * arrayA[i + 1]\n",
    "        for i in range(n):\n",
    "            arrayA[i] = left[i] * right[i]\n",
    "        return arrayA\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        left = [1] * len(arrayA)\n",
    "        right = [1] * len(arrayA)\n",
    "        for i in range(1, len(arrayA)):\n",
    "            left[i] = left[i-1] * arrayA[i-1]\n",
    "        for j in range(len(arrayA)-2, -1, -1):\n",
    "            right[j] = right[j+1] * arrayA[j+1]\n",
    "        for k in range(len(arrayA)):\n",
    "            arrayA[k] = left[k] * right[k]\n",
    "        return arrayA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        b1 = [1] * n\n",
    "        b2 = [1] * n\n",
    "        for i in range(1,n):\n",
    "            b1[i] = b1[i-1] * arrayA[i-1]\n",
    "            b2[n-i-1] = arrayA[n-i] * b2[n-i]\n",
    "        for i in range(n):\n",
    "            b1[i] = b1[i] * b2[i]\n",
    "        return b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:#前后缀和\n",
    "        ps=[1]\n",
    "        for i in arrayA[::-1]:\n",
    "            ps.append(ps[-1]*i)\n",
    "        ps.reverse()\n",
    "        prev=1\n",
    "        n=len(arrayA)\n",
    "        res=[1]*n\n",
    "        for i in range(n):\n",
    "            res[i]=prev*ps[i+1]\n",
    "            prev*=arrayA[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        L, R = [1] * n, [1] * n\n",
    "        for i in range(1, n):\n",
    "            L[i] = L[i - 1] * arrayA[i - 1]\n",
    "        for j in reversed(range(n - 1)):\n",
    "            R[j] = R[j + 1] * arrayA[j + 1]\n",
    "        for i in range(n):\n",
    "            L[i] = L[i] * R[i]\n",
    "        return L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "\n",
    "        if not arrayA:\n",
    "            return arrayA\n",
    "        \n",
    "        n = len(arrayA)\n",
    "\n",
    "        mul_l = [1] + [0] * n\n",
    "        mul_r = [0] * n + [1]\n",
    "\n",
    "        for i in range(1, n+1):\n",
    "            mul_l[i] = mul_l[i-1] * arrayA[i-1]\n",
    "            mul_r[n - i] = mul_r[n - i + 1] * arrayA[n - i]\n",
    "        \n",
    "        ret = [0] * n\n",
    "\n",
    "        for i in range(n):\n",
    "            ret[i] = mul_l[i] * mul_r[i+1]\n",
    "\n",
    "        # from ans, could optimize to save spce to O(1)\n",
    "\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "\n",
    "        if not arrayA:\n",
    "            return arrayA\n",
    "        \n",
    "        n = len(arrayA)\n",
    "\n",
    "        mul_l = [1] + [0] * n\n",
    "        mul_r = [0] * n + [1]\n",
    "\n",
    "        for i in range(1, n+1):\n",
    "            mul_l[i] = mul_l[i-1] * arrayA[i-1]\n",
    "            mul_r[n - i] = mul_r[n - i + 1] * arrayA[n - i]\n",
    "        \n",
    "        ret = [0] * n\n",
    "\n",
    "        for i in range(n):\n",
    "            ret[i] = mul_l[i] * mul_r[i+1]\n",
    "\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        left, right = [1], [1]\n",
    "        for i in range(n):\n",
    "            left.append(left[-1] * arrayA[i])\n",
    "            right.append(right[-1] * arrayA[-i-1])\n",
    "        return [left[i] * right[-i-2] for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        if not arrayA:\n",
    "            return []\n",
    "        B = [0]*len(arrayA)\n",
    "        left = [1]*len(arrayA)\n",
    "        right = [1]*len(arrayA)\n",
    "        left[0] = 1\n",
    "        right[len(arrayA)-1] = 1\n",
    "        for i in range(1,len(arrayA)):\n",
    "            left[i] = left[i-1]*arrayA[i-1]\n",
    "        for j in range(len(arrayA)-2,-1,-1):\n",
    "            right[j] = right[j+1]*arrayA[j+1]\n",
    "        for n in range(len(arrayA)):\n",
    "            B[n] = left[n]*right[n]\n",
    "#            B.append(left[n]*right[n])  \n",
    "        return B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        prod1 = [1] * (n + 1)\n",
    "        prod2 = [1] * (n + 1)\n",
    "        for i in range(n):\n",
    "            prod1[i + 1] = prod1[i] * arrayA[i]\n",
    "        for i in range(n):\n",
    "            prod2[i + 1] = prod2[i] * arrayA[n - i - 1]\n",
    "        return [prod1[i] * prod2[-i-2] for i in range(n)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        left, right = [0]*(n), [0]*(n)\n",
    "        left_multi, right_multi = 1, 1\n",
    "        for i in range(n):\n",
    "            left[i] = left_multi\n",
    "            left_multi *= arrayA[i]\n",
    "            right[-i-1] = right_multi\n",
    "            right_multi *= arrayA[-i-1]\n",
    "        result = []\n",
    "        for i in range(n):\n",
    "            result.append(left[i]*right[i])\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        left = [1] * (n + 1)\n",
    "        for i in range(1, n + 1):\n",
    "            left[i] = left[i - 1] * arrayA[i - 1]\n",
    "        right = [1] * (n + 1)\n",
    "        for i in range(n - 1, - 1, -1):\n",
    "            right[i] = right[i + 1] * arrayA[i]\n",
    "        arrayB = [0] * n\n",
    "        for i in range(n):\n",
    "            arrayB[i] = left[i] * right[i + 1]\n",
    "        return arrayB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        \n",
    "        n = len(arrayA)\n",
    "        lmul = [0] * n\n",
    "        rmul = [0] * n\n",
    "        if n == 0: return []\n",
    "        tmp = 1\n",
    "        for i in range(n-1):\n",
    "            tmp *= arrayA[i]\n",
    "            lmul[i] = tmp\n",
    "        tmp = 1\n",
    "        for j in range(n-1, 0, -1):\n",
    "            tmp *= arrayA[j]\n",
    "            rmul[j] = tmp\n",
    "        res = [0] * n\n",
    "        for k in range(n):\n",
    "            if k == 0:\n",
    "                res[k] = rmul[1]\n",
    "            elif k == n-1:\n",
    "                res[k] = lmul[-2]\n",
    "            else:\n",
    "                res[k] = lmul[k-1]*rmul[k+1]\n",
    "        return res\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        n = len(arrayA)\n",
    "        if not arrayA:\n",
    "            return []\n",
    "        pre_prod = [arrayA[0]]\n",
    "        suf_prod = [arrayA[-1]]\n",
    "        for i in range(1, n-1):\n",
    "            pre_prod.append(pre_prod[-1] * arrayA[i])\n",
    "        for i in range(n-2, 0, -1):\n",
    "            suf_prod.append(suf_prod[-1] * arrayA[i])\n",
    "\n",
    "        \n",
    "        res = [suf_prod[-1]]\n",
    "        for i in range(1, n-1):\n",
    "            res.append(pre_prod[i-1] * suf_prod[n-i-2])\n",
    "        res.append(pre_prod[-1])\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def statisticalResult(self, arrayA: List[int]) -> List[int]:\n",
    "        dpleft = [1]*len(arrayA)\n",
    "        dpright = [1]*len(arrayA)\n",
    "        dp = [1]*len(arrayA)\n",
    "        for i in range(1,len(arrayA)):\n",
    "            dpleft[i] = arrayA[i-1]*dpleft[i-1]\n",
    "        for i in range(len(arrayA)-2,-1,-1):\n",
    "            print(arrayA[i+1],dpright[i+1])\n",
    "            dpright[i] = arrayA[i+1]*dpright[i+1]\n",
    "        print(dpleft)\n",
    "        print(dpright)\n",
    "        for i in range(0,len(arrayA)):\n",
    "            dp[i] = dpleft[i] * dpright[i]\n",
    "        return dp\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
